package cn.wolfcode.crm.web.controller;


import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.service.IRoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IPermissionService permissionService;
    @Autowired
    private IRoleService roleService;
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")QueryObject qo){
        PageInfo result = roleService.query(qo);
        model.addAttribute("result",result);
        return "/role/list";
    }
    @RequestMapping("/delete")
    public String delete(Long id){
        roleService.delete(id);
        return "redirect:/role/list.do";
    }
    
    @RequestMapping("/input")
    public String input(Model model,Long id){
        //共享权限数据
        List<Permission> list = permissionService.list();
        model.addAttribute("permissions",list);
        if(id != null){
            Role role = roleService.get(id);
            model.addAttribute("role",role);
        }
        return "role/input";
    }

    @RequestMapping("saveOrUpdate")
    public String saveOrUpdate(Role role,Long[] ids){
        if(role.getId() != null){
            roleService.update(role,ids);
        }else{
            roleService.save(role,ids);
        }
        return "redirect:/role/list.do";
    }
}
